<!--广告类型 -->
<template>
  <div class="table">
    <div class="container">
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>
            <el-icon>
              <Search />
            </el-icon> 查询条件1：
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row :gutter="10">
        <el-col :xl="6" :lg="6" :md="12" :sm="24" style="margin-bottom:10px">
          <el-input v-model.trim="searchCondition.type_name" placeholder="分类名称" clearable></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" style="margin-bottom:10px">
          <el-button type="success" :icon="Search" :disabled="disabledbtn" @click="searchInfo">搜索</el-button>
          <el-button type="warning" :icon="Refresh" :disabled="disabledbtn" @click="resetSearch">重置</el-button>
        </el-col>
      </el-row>
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 查询结果：</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="handle-box">
        <el-button type="primary" :icon="Plus" class="mr10" :disabled="disabledbtn" @click="editVisible = true">新增
        </el-button>
        <el-popconfirm placement="top" width="160" title="你确定要删除吗?" @confirm="batchDelete('deleteBannerTypeInfo')">
          <template #reference>
            <el-button type="danger" :icon="Delete" class="mr10" :disabled="disabledbtn">批量删除
            </el-button>
          </template>
        </el-popconfirm>
      </div>
      <el-table :data="tableState.tableList.slice((pageNumber - 1) * pageSize, pageNumber * pageSize)" border
        class="table" ref="multipleTableRef" v-loading="tableLoading" :reserve-selection="true"
        @row-dblclick="rowDblclick">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="type_name" label="页面名称" align="center">
        </el-table-column>
        <el-table-column prop="banner_link" label="跳转链接" align="center">
          <template #default="scope">
            {{ [null, ''].includes(scope.row.banner_link) ? '--' : scope.row.banner_link }}
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" align="center">
          <template #default="scope">
            <div class="">
              <el-input v-model.trim.number="scope.row.sort" placeholder="请输入内容" @change="handleSort(scope.row)">
              </el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" text :icon="View" @click="handleViewEdit(scope.row, 'view', '查看')">
              查看
            </el-button>
            <el-button type="primary" size="small" text :icon="Edit" @click="handleViewEdit(scope.row, 'edit', '编辑')">
              编辑
            </el-button>
            <div class="mlr10 display-inline-block">
              <el-popconfirm placement="top" width="160" title="你确定要删除吗?"
                @confirm="deleteInfo('deleteBannerTypeInfo', [id])">
                <template #reference>
                  <el-button text :icon="Delete" size="small" class="red" @click="id = scope.row.id">删除
                  </el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background @update:current-page="handleCurrentChange" @update:page-size="handleSizeChange"
          layout="total, sizes, prev, pager, next, jumper" :page-size="pageSize" :current-page="pageNumber"
          :page-sizes="pageSizes" :total="total" :hide-on-single-page="false">
        </el-pagination>
      </div>
    </div>

    <!-- 新增编辑查看弹出框 -->
    <el-dialog :title="dialogTitle" v-model="editVisible" width="60%" draggable
      :close-on-click-modal="closeOnClickModal" @closed="closedFormDialog">
      <el-form ref="advertisingTypeFormRef" :model="advertisingTypeForm" :rules="rules" v-loading="formLoading"
        label-width="150px">
        <el-row :gutter="10">
          <el-col :span="10">
            <el-form-item label="页面名称：" prop="type_name">
              <el-input v-model.trim="advertisingTypeForm.type_name" placeholder="页面名称" clearable>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="排序：" prop="sort">
              <el-input v-model.trim.number="advertisingTypeForm.sort" placeholder="排序" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="10">
            <el-form-item label="跳转链接：" prop="banner_link">
              <el-input v-model.trim.number="advertisingTypeForm.banner_link" placeholder="跳转链接" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" :disabled="disabledbtn" v-if="!isDisabledbtn"
            @click="submitForm('commonBannerTypeInfo', getTableInfo)">
            确 定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import { View, Delete, Edit, Search, Plus, Refresh } from '@element-plus/icons-vue';
import { api as Request } from '@/utils/api';

import { useTable } from '@/hooks/useTable.js';
import { useDialogForm } from '@/hooks/useDialogForm.js';
import { useSort } from '@/hooks/useSort.js';
import useRules from '@/hooks/useRules.js';

const disabledbtn = ref(false);/* 按钮可用与不可用 */
const formLoading = ref(false);/* 表单loading状态 */
let { total, pageNumber, pageSize, pageSizes, multipleTableRef, tableLoading, tableState, searchCondition,
  handleCurrentChange, handleSizeChange, getTableInfo, searchInfo, resetSearch, batchDelete, deleteInfo, rowDblclick } =
  useTable({
    type_name: ''
  }, 'getBannerTypeInfo', disabledbtn, 'FRONT_END');

let { formRef: advertisingTypeFormRef, formData: advertisingTypeForm, methodType, isDisabledbtn,
  closeOnClickModal, dialogTitle, editVisible, closedFormDialog, submitForm } = useDialogForm({
    id: '',
    type_name: '',
    banner_link: '',
    sort: 0
  }, disabledbtn, handleCustomOperation, formLoading);
let { handleSort } = useSort('putBannerTypeSortInfo', searchInfo);

const rules = useRules('advertisingTypeRules');/* 校验规则 */
const id = ref(-1); /* 删除时所需的编号 删除时的id */

/* 点击编辑按钮 */
const handleViewEdit = (row, code, name) => {
  editVisible.value = true;
  if ('view' === code) {
    dialogTitle.value = name;
    isDisabledbtn.value = true;
    closeOnClickModal.value = true;
  } else if ('edit' === code) {
    dialogTitle.value = name;
    methodType.value = 'PUT';
  }
  /* 为了可以使用表单重置的方法 */
  nextTick(() => {
    advertisingTypeForm.id = row.id;
    advertisingTypeForm.banner_link = row.banner_link;
    advertisingTypeForm.type_name = row.type_name;
    advertisingTypeForm.sort = row.sort;
  });
}

/*关闭模态框以后的自定义操作 */
function handleCustomOperation () {
  advertisingTypeForm.id = '';/* 这个还需要手动清除 */
  console.log(advertisingTypeForm);
}
/* 获取表单信息 */
getTableInfo();
</script>
<style lang="scss" scoped>
/* layout-布局 */
.el-row {
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0;
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.textAlignCenter {
  text-align: center;
}

/* 修改select的盒模型 */
.el-select {
  display: block;
}

/* 确定按钮的显示与隐藏 */
.displaybtn {
  display: none;
}

/* 修改该属性值  查询条件，查询结果得样式 */
.el-breadcrumb {
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 10px;
}

/* 提示图标设置 */
.icon-wzg-config {
  height: 30px;
  line-height: 30px;
}

/* 修改Switch开关 */
.el-switch {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}
</style>
